package janelas;


import Classes.Sistema;
import javax.swing.Icon;
import javax.swing.JOptionPane;

/**
 *
 * @author Daniela
 */
public class CadastroDeUsuario extends javax.swing.JFrame {

    private JanelaPrincipal janelaAnterior;
    public static Icon foto;
    public static String nome, login, senha1;
    public static int sexo;
    private boolean fotoPadrao;

    public CadastroDeUsuario(JanelaPrincipal janelaAnterior) {
        initComponents();
        setResizable(false); 
        setLocationRelativeTo(null);
        this.janelaAnterior = janelaAnterior;
        Sistema sistema = janelaAnterior.getSistema();
        
        foto = labelFoto.getIcon();
        fotoPadrao = true;
    }
    

    public Sistema getSistema() { 
        return janelaAnterior.getSistema();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jPasswordField2 = new javax.swing.JPasswordField();
        jToggleButton1 = new javax.swing.JToggleButton();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        campoSenha2 = new javax.swing.JPasswordField();
        campoSenha1 = new javax.swing.JPasswordField();
        campoLogin = new javax.swing.JTextField();
        comboxSexo = new javax.swing.JComboBox();
        campoNome = new javax.swing.JTextField();
        comboxUsuario = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        bAvançar = new javax.swing.JButton();
        bCancelar = new javax.swing.JButton();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0));
        jLabel7 = new javax.swing.JLabel();
        bProcurarFoto = new javax.swing.JToggleButton();
        labelFoto = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        jTextField1.setText("jTextField1");

        jPasswordField2.setText("jPasswordField2");

        jToggleButton1.setText("jToggleButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro de Usuário");
        setBackground(new java.awt.Color(204, 204, 204));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(153, 153, 255));
        jLabel1.setText("Criando sua Conta Social-Libary-DSC");

        jPanel1.setBackground(new java.awt.Color(204, 204, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel2.setText("Tipo de Usuário");

        jLabel8.setText("Nome completo ");

        jLabel9.setText("Sexo");

        jLabel11.setText("Criar um login");

        jLabel3.setText("Criar uma senha");

        jLabel4.setText("Redigitar senha");

        campoSenha2.setToolTipText("Redigite a senha");

        campoSenha1.setToolTipText("Crie uma senha");
        campoSenha1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoSenha1ActionPerformed(evt);
            }
        });

        campoLogin.setToolTipText("Crie um nome para acessar sua conta");

        comboxSexo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione uma opção", "Feminino", "Masculino" }));
        comboxSexo.setToolTipText("Selecione o sexo");
        comboxSexo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                comboxSexoItemStateChanged(evt);
            }
        });
        comboxSexo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboxSexoActionPerformed(evt);
            }
        });

        campoNome.setToolTipText("Digite seu nome completo");
        campoNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoNomeActionPerformed(evt);
            }
        });

        comboxUsuario.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione uma opção", "Professor" }));
        comboxUsuario.setToolTipText("Que tipo de usuário você é?");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 9)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(153, 153, 153));
        jLabel5.setText("Mínimo 6 caracteres");

        bAvançar.setText("Avançar");
        bAvançar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bAvançarMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                bAvançarMouseExited(evt);
            }
        });
        bAvançar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAvançarActionPerformed(evt);
            }
        });

        bCancelar.setText("Cancelar");
        bCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCancelarActionPerformed(evt);
            }
        });

        bProcurarFoto.setText("Procurar...");
        bProcurarFoto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bProcurarFotoActionPerformed(evt);
            }
        });

        labelFoto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/janelas/masculino-usuario-clip-art-icon_419253.jpg"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(labelFoto, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel8)
                                        .addGap(18, 18, 18)
                                        .addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, 359, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(filler1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel11, javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.LEADING))
                                        .addGap(18, 18, 18)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                .addComponent(campoLogin)
                                                .addComponent(campoSenha1)
                                                .addComponent(comboxSexo, 0, 142, Short.MAX_VALUE))
                                            .addComponent(jLabel5)
                                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(bCancelar)
                                                .addComponent(comboxUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(campoSenha2, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(97, 97, 97)
                                                .addComponent(bProcurarFoto))))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(96, 96, 96)
                                        .addComponent(jLabel7)))
                                .addGap(128, 128, 128))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(78, 78, 78)
                        .addComponent(bAvançar)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(comboxSexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11)
                            .addComponent(campoLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(26, 26, 26)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(campoSenha1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(8, 8, 8)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(campoSenha2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addGap(24, 24, 24)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(comboxUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(31, 31, 31)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(bAvançar)
                            .addComponent(bCancelar))
                        .addGap(271, 271, 271)
                        .addComponent(jLabel7)
                        .addGap(175, 175, 175)
                        .addComponent(filler1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addComponent(labelFoto, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(bProcurarFoto))))
        );

        jLabel6.setForeground(new java.awt.Color(255, 0, 0));
        jLabel6.setText("* O preenchimento de todos os campos é obrigatório");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 372, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 496, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 341, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        janelaAnterior.setVisible(true);
        janelaAnterior.setEnabled(true);
        
    }//GEN-LAST:event_formWindowClosed

    private void bProcurarFotoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bProcurarFotoActionPerformed
        new JanelaRecebeFoto(this).setVisible(true);
    }//GEN-LAST:event_bProcurarFotoActionPerformed

    private void bCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelarActionPerformed
        this.dispose();
        janelaAnterior.setEnabled(true);
    }//GEN-LAST:event_bCancelarActionPerformed

    private void bAvançarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAvançarActionPerformed
      
    }//GEN-LAST:event_bAvançarActionPerformed

    private void bAvançarMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bAvançarMouseExited
        // TODO add your handling code here:
    }//GEN-LAST:event_bAvançarMouseExited

    private void bAvançarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bAvançarMouseClicked
        String mensagensDeErro = new String("");
        nome = campoNome.getText();
        sexo = comboxSexo.getSelectedIndex();
        login = campoLogin.getText();
        senha1 = String.valueOf(campoSenha1.getPassword());
        String senha2 = String.valueOf(campoSenha2.getPassword());
        int usuario = comboxUsuario.getSelectedIndex();
        
        if(nome.replaceAll(" ", "").equals("") && sexo == 0 && login.replaceAll(" ", "").equals("") &&
           senha1.replaceAll(" ", "").equals("") && usuario == 0){
            mensagensDeErro += "Todos os campos devem ser preenchidos!";
        }else{
            if(nome.replaceAll(" ", "").equals("")){
                mensagensDeErro += "O campo nome não pode ser vazio!\n";
            }if(sexo == 0){
                mensagensDeErro += "O campo sexo não foi selecionado!\n";
            }if(login.replaceAll(" ", "").equals("")){
                mensagensDeErro += "O campo login não pode ser vazio!\n";
            }if(!(janelaAnterior.getSistema().validaUserName(login))){
                mensagensDeErro += "Nome de Usuário já existe!\n";
                campoLogin.setText("");
            }if(senha1.replaceAll(" ", "").equals("")){
                mensagensDeErro += "O campo senha não pode ser vazio!\n";
            }else if(senha1.replaceAll(" ", "").length() < 6){
                mensagensDeErro += "A senha deve conter no mínimo 6 caracteres!\n";
                campoSenha1.setText("");
                campoSenha2.setText("");
            }else if(!senha1.equals(senha2)){
                mensagensDeErro += "Senhas não correspondem!\n";
                campoSenha1.setText("");
                campoSenha2.setText("");
            }if(usuario == 0){
                mensagensDeErro += "O campo tipo de usuário não foi selecionado!\n";
            }
        }
        
        if(mensagensDeErro.equals("")){
            new CadastrarProfessor(this).setVisible(true);
            this.setVisible(false);
        }else
            JOptionPane.showMessageDialog(null, mensagensDeErro);
    }//GEN-LAST:event_bAvançarMouseClicked

    private void campoNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoNomeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoNomeActionPerformed

    private void comboxSexoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_comboxSexoItemStateChanged
        if (comboxSexo.getSelectedIndex() == 2 && fotoPadrao) {
            labelFoto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/janelas/masculino-usuario-clip-art-icon_419253.jpg"))); // NOI18N
            foto = labelFoto.getIcon();
        } else if (comboxSexo.getSelectedIndex() == 1 && fotoPadrao) {
            labelFoto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/janelas/download.jpg"))); // NOI18N
            foto = labelFoto.getIcon();
        }
    }//GEN-LAST:event_comboxSexoItemStateChanged

    private void campoSenha1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoSenha1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoSenha1ActionPerformed

    private void comboxSexoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboxSexoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_comboxSexoActionPerformed

    public void recebeFoto(Icon foto) {
        this.foto = foto;
        fotoPadrao = false;
        labelFoto.setIcon(foto);
    }
    
   public void chamaPrincipal() { 
        janelaAnterior.setVisible(true);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bAvançar;
    private javax.swing.JButton bCancelar;
    private javax.swing.JToggleButton bProcurarFoto;
    private javax.swing.JTextField campoLogin;
    private javax.swing.JTextField campoNome;
    private javax.swing.JPasswordField campoSenha1;
    private javax.swing.JPasswordField campoSenha2;
    private javax.swing.JComboBox comboxSexo;
    private javax.swing.JComboBox comboxUsuario;
    private javax.swing.Box.Filler filler1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField jPasswordField2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JLabel labelFoto;
    // End of variables declaration//GEN-END:variables
}
